//
//  ApplyViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/7.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "ApplyViewController.h"
#import "ExperienceTextfield.h"
#import "TPKeyboardAvoidingScrollView.h"
@interface ApplyViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *aview;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) ExperienceTextfield *nameTextfield;
@property (nonatomic, strong) ExperienceTextfield *phoneNumberTextfield;

@property (nonatomic, strong) TPKeyboardAvoidingScrollView *backgroundView;
@property (nonatomic, strong) UILabel *peopleLabel;
@end

@implementation ApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackItem];
    self.title = @"填写申请";
    [self createView];
//    [self addobServer];//监听键盘弹出
    [self createReminderView];//弹出框
    [self startNetwork];
}

- (void)startNetwork
{
    [NetWork sendGetRequestURL:[NSString stringWithFormat:@"%@/experience/CommitExpApply?eid=%@", GW_Domain, self.strId] parameters:@{@"eid":self.strId} completionBlockWithSuccess:^(NSDictionary *resultDict) {
        NSLog(@"---%@", resultDict);
        NSDictionary *dict = resultDict[@"data"];
        @weakobj(self);
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongobj(self);
            self.titleLabel.text = dict[@"goods_name"];
            self.peopleLabel.text = dict[@"store_address"];
            self.dateLabel.text = [NSString stringWithFormat:@"%@-%@", dict[@"start_time"], dict[@"end_time"]];
        });
        
    } andFailure:^(NSError *error, NSString *errorMsg) {
        MESSAGE_TOAST(errorMsg);
        NSLog(@"%@", errorMsg);
    }];
}

- (void) addobServer
{
    //使用NSNotificationCenter 监听键盘出现
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShown:)
                                                 name:UIKeyboardWillChangeFrameNotification object:nil];
    //使用NSNotificationCenter 鍵盤隐藏時
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)createView
{
    
    
    _backgroundView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, -64, GWIDTH, GHEIGHT)];
    [self.view addSubview:_backgroundView];
    if(IS_IPHONE5)
        _backgroundView.contentSize = CGSizeMake(0, GHEIGHT + 80);
    
    UIImageView *topImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, GWIDTH, 137)];
    topImage.backgroundColor = ColorYellow;
    topImage.image = IMAGE(@"experience_apply_image");
    [_backgroundView addSubview:topImage];
    
    UIView *view = [UIView new];
    view.layer.shadowColor = ColorFrom16(0xa5a5a5).CGColor;
    view.layer.shadowRadius = 4;
    view.layer.shadowOpacity = 0.8;
    view.layer.shadowOffset = CGSizeMake(0, 0);
    view.layer.backgroundColor = ColorFrom16(0xffffff).CGColor;
    view.layer.cornerRadius = 2;
    view.layer.cornerRadius = 6;
    [_backgroundView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImage.mas_bottom).offset(-16);
        make.width.equalTo(@325);
        make.height.equalTo(@140);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
    UILabel *circle = [UILabel new];
    circle.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    circle.layer.cornerRadius = 3;
    [view addSubview:circle];
    [circle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).offset(5);
        make.left.equalTo(view).offset(5);
        make.width.equalTo(@6);
        make.height.equalTo(@6);
    }];
    
    UILabel *line = [UILabel new];
    line.backgroundColor = ColorFrom16(0xFEDE6D);
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).offset(7);
        make.left.equalTo(circle.mas_right);
        make.width.equalTo(@305);
        make.height.equalTo(@1);
    }];
    
    UILabel *circleT = [UILabel new];
    circleT.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    circleT.layer.cornerRadius = 3;
    [view addSubview:circleT];
    [circleT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).offset(5);
        make.left.equalTo(line.mas_right);
        make.width.equalTo(@6);
        make.height.equalTo(@6);
    }];
    
    UILabel *nameLabel = [UILabel new];
    nameLabel.text = @"名称";
    nameLabel.textColor = ColorFrom16(0x7F7E7D);
    nameLabel.font = PFRFONT(10);
    [view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(11);
        make.left.equalTo(view).offset(15);
        make.width.greaterThanOrEqualTo(@34);
        make.height.equalTo(@14);
    }];
    
    _titleLabel = [UILabel new];
    _titleLabel.text = @"爱儿美儿童舞蹈秋季梦想儿童舞蹈...";
    _titleLabel.textColor = ColorFrom16(0x333333);
    _titleLabel.font = PFRFONT(12);
    [view addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(9);
        make.left.equalTo(view).offset(80);
        make.width.equalTo(@205);
        make.height.equalTo(@17);
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = ColorFrom16(0xEEF4F7);
    [view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(12);
        make.left.equalTo(view).offset(15);
        make.right.equalTo(view).offset(-15);
        make.height.equalTo(@1);
    }];
    
    UILabel *peopleNum = [UILabel new];
    peopleNum.text = @"店铺地址";
    peopleNum.textColor = ColorFrom16(0x7F7E7D);
    peopleNum.font = PFRFONT(10);
    [view addSubview:peopleNum];
    [peopleNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(11);
        make.left.equalTo(view).offset(15);
        make.width.greaterThanOrEqualTo(@34);
        make.height.equalTo(@14);
    }];
    
    _peopleLabel = [UILabel new];
    _peopleLabel.text = @"成人1名  儿童1名";
    _peopleLabel.textColor = ColorFrom16(0x333333);
    _peopleLabel.font = PFRFONT(12);
    [view addSubview:_peopleLabel];
    [_peopleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(9);
        make.left.equalTo(view).offset(80);
        make.width.equalTo(@205);
        make.height.equalTo(@17);
    }];
    
    
    UIView *lineView1 = [UIView new];
    lineView1.backgroundColor = ColorFrom16(0xEEF4F7);
    [view addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_peopleLabel.mas_bottom).offset(12);
        make.left.equalTo(view).offset(15);
        make.right.equalTo(view).offset(-15);
        make.height.equalTo(@1);
    }];
    
    UILabel *date = [UILabel new];
    date.text = @"活动时间";
    date.textColor = ColorFrom16(0x7F7E7D);
    date.font = PFRFONT(10);
    [view addSubview:date];
    [date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView1.mas_bottom).offset(11);
        make.left.equalTo(view).offset(15);
        make.width.greaterThanOrEqualTo(@34);
        make.height.equalTo(@14);
    }];
    
    _dateLabel = [UILabel new];
    _dateLabel.text = @"9月17日 10:30-9月19日  11:30";
    _dateLabel.textColor = ColorFrom16(0x333333);
    _dateLabel.font = PFRFONT(12);
    [view addSubview:_dateLabel];
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView1.mas_bottom).offset(9);
        make.left.equalTo(view).offset(80);
        make.width.equalTo(@205);
        make.height.equalTo(@17);
    }];
    
    
    //联系人
    UILabel *contactLabel = [UILabel new];
    contactLabel.text = @"联系人信息";
    contactLabel.textColor = ColorFrom16(0x333333);
    contactLabel.font = PFRFONT(14);
    [_backgroundView addSubview:contactLabel];
    [contactLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom).offset(48);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(@70);
        make.height.equalTo(@20);
    }];
    
    
    //左侧占位图片
    UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 8, 20, 20)];
    leftView.image = IMAGE(@"experience_username");
    
    UIView *lef = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 38)];
    [lef addSubview:leftView];
    
    _nameTextfield = [ExperienceTextfield new];
    _nameTextfield.placeholder = @"请输入联系人姓名";
    [_nameTextfield setValue:ColorFrom16A(0xa4a4a4, 1) forKeyPath:@"_placeholderLabel.textColor"];//通过kvc去改变占位符的字体颜色
    [_nameTextfield setValue:PFRFONT(12) forKeyPath:@"_placeholderLabel.font"];
    _nameTextfield.leftView = lef;
    _nameTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
    _nameTextfield.leftViewMode = UITextFieldViewModeAlways;
    _nameTextfield.layer.backgroundColor = ColorFrom16(0xffffff).CGColor;
    _nameTextfield.layer.cornerRadius = 7;
    _nameTextfield.layer.borderColor = ColorFrom16(0xF5CB37).CGColor;
    _nameTextfield.layer.borderWidth = 1;
    _nameTextfield.autocapitalizationType = UITextAutocapitalizationTypeNone;//首字母不自动大写
    _nameTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    _nameTextfield.text = USER_NAME;
    [_backgroundView addSubview:_nameTextfield];
    [_nameTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contactLabel.mas_bottom).offset(21);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@38);
    }];
    
    
    //占位符 图片
    UIImageView *leftV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 8, 20, 20)];
    leftV.image = IMAGE(@"experience_phone_number");
    
    UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 38)];
    [aview addSubview:leftV];
    
    _phoneNumberTextfield = [ExperienceTextfield new];
    _phoneNumberTextfield.placeholder = @"请输入联系人手机号";
    [_phoneNumberTextfield setValue:ColorFrom16A(0xa4a4a4, 1) forKeyPath:@"_placeholderLabel.textColor"];//通过kvc去改变占位符的字体颜色
    [_phoneNumberTextfield setValue:PFRFONT(12) forKeyPath:@"_placeholderLabel.font"];
    _phoneNumberTextfield.leftView = aview;
    _phoneNumberTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
    _phoneNumberTextfield.leftViewMode = UITextFieldViewModeAlways;
    _phoneNumberTextfield.layer.backgroundColor = ColorFrom16(0xffffff).CGColor;
    _phoneNumberTextfield.layer.cornerRadius = 7;
    _phoneNumberTextfield.layer.borderColor = ColorFrom16(0xF5CB37).CGColor;
    _phoneNumberTextfield.layer.borderWidth = 1;
    _phoneNumberTextfield.keyboardType = UIKeyboardTypeNumberPad;
    _phoneNumberTextfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneNumberTextfield.text = USER_TEL;
    [_backgroundView addSubview:_phoneNumberTextfield];
    [_phoneNumberTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameTextfield.mas_bottom).offset(22);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@38);
    }];
    
    //免费申请按钮
    UIButton *applyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [applyBtn setTitle:@"免费申请" forState:0];
    [applyBtn setTitleColor:ColorFrom16(0x202020) forState:0];
    applyBtn.titleLabel.font = PFRFONT(12);
    applyBtn.layer.backgroundColor = ColorFrom16(0xFEDE6D).CGColor;
    applyBtn.layer.cornerRadius = 7;
    [applyBtn addTarget:self action:@selector(handleApplyBtn) forControlEvents:UIControlEventTouchUpInside];
    [_backgroundView addSubview:applyBtn];
    [applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneNumberTextfield.mas_bottom).offset(50);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
        make.height.equalTo(@44);
    }];
}

#pragma mark - 免费申请响应方法
- (void)handleApplyBtn
{
    
    if([GW_Tool isEmptyString:_nameTextfield.text] || [GW_Tool isEmptyString:_phoneNumberTextfield.text])
    {
        MESSAGE_TOAST(@"请填写姓名和电话号码");
        return;
    }
    [self.view endEditing:YES];
    NSDictionary *param = @{@"eid": self.strId,
                            @"apply_name": _nameTextfield.text,
                            @"apply_phone": _phoneNumberTextfield.text};
    [NetWork sendPostRequestURL:[NSString stringWithFormat:@"%@/experience/CommitExpApply", GW_Domain] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        MESSAGE_TOAST(resultDict[@"message"])
        
        [UIView animateWithDuration:1.0 animations:^{
            _bgView.hidden = NO;
        }];
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", errorMsg);
        MESSAGE_TOAST(errorMsg);
    }];
}

//键盘弹出
- (void)keyboardWillShown:(NSNotification*)aNotification
{
    NSDictionary *info = [aNotification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    //    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    //    CGSize keyboardSize = [value CGRectValue].size;
    
    //输入框位置动画加载
    [UIView animateWithDuration:duration animations:^{
        //do something
    }];
    
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    //上移55个单位，按实际情况设置
    CGRect rect=CGRectMake(0.0f, -55, GWIDTH, GHEIGHT);
    self.view.frame = rect;
    [UIView commitAnimations];
    
}
//键盘隐藏
- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    //如果当前View是父视图，则Y为20个像素高度，如果当前View为其他View的子视图，则动态调节Y的高度
    //    float Y = 20.0f;
    CGRect rect=CGRectMake(0.0f,55,GWIDTH,GHEIGHT);
    self.view.frame = rect;
    [UIView commitAnimations];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//弹出框
- (void)createReminderView
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    _bgView.backgroundColor = ColorFrom16A(0x000000, 0.8);
    _bgView.hidden = YES;
    [self.navigationController.view addSubview:_bgView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction)];
    tap.delegate = self;
    [_bgView addGestureRecognizer:tap];
    
    _aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 180)];
    _aview.layer.backgroundColor = ColorWhite.CGColor;
    _aview.layer.cornerRadius = 12;
    _aview.center = _bgView.center;
    [_bgView addSubview:_aview];
    
    UIImageView *iv = [UIImageView new];
    iv.image = IMAGE(@"correct");
    iv.layer.cornerRadius = 67 / 2;
    [_aview addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_aview).offset(-29);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.equalTo(@67);
        make.height.equalTo(@67);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"您已申请完成";
    titleLabel.textColor = ColorFrom16(0xF5CB37);
    titleLabel.font = PFRFONT(17);
    [_aview addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iv.mas_bottom).offset(24);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.greaterThanOrEqualTo(@100);
        make.height.equalTo(@24);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"审核会在24小时之内完成。审核进度，您可到“体验-已报名”中进行查看。";
    label.textColor = ColorFrom16(0xA7A7A7);
    label.numberOfLines = 2;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = 1;
    label.font = PFRFONT(13);
    [_aview addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.width.equalTo(@240);
        make.height.equalTo(@32);
        make.centerX.equalTo(_aview.mas_centerX);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"立即查看" forState:0];
    [button setTitleColor:ColorFrom16(0x323232) forState:0];
    button.titleLabel.font = PFRFONT(17);
    button.backgroundColor = ColorFrom16(0xFEDE6D);
    [_aview addSubview:button];
    button.frame = CGRectMake(0, 140, 270, 40);
    [button addTarget:self action:@selector(handlebtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //切圆角  任意切两个
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = button.bounds;
    maskLayer.path = maskPath.CGPath;
    button.layer.mask = maskLayer;
}

//立即查看
- (void)handlebtn:(UIButton *)sender
{
    _bgView.hidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoSee" object:nil];
}

//tap手势响应方法
- (void)handleTapAction
{
    [UIView animateWithDuration:1.0 animations:^{
        _bgView.hidden = YES;
    }];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isDescendantOfView:_aview])
        return NO;
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
